package com.fafa.swing.base;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: JTreeDemo
 * @date Date : 2019年11月26日 22:35
 */
public class JTreeDemo {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("教师学历信息");
        jFrame.setBounds(500,500,400,300);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.getContentPane().add(new JTreeDemo().createComponent());
        jFrame.pack();
        jFrame.setVisible(true);
    }

    private JPanel createComponent(){
        JPanel jPanel = new JPanel();
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("教师学历信息");
        String[][] teachers = new String[3][];
        teachers[0] = new String[]{"王鹏","李曼","韩小国","穆保龄","尚凌云","范超峰"};
        teachers[1] = new String[]{"胡会强","张春辉","宋芳","阳芳","朱山根","张茜","宋媛媛"};
        teachers[2] = new String[]{"刘丹","张小芳","刘华亮","聂来","吴琼"};
        String gradeNames[] = {"硕士学历","博士学历","博士后学历"};
        DefaultMutableTreeNode node = null;
        DefaultMutableTreeNode childNode = null;
        int length = 0;
        for (int i = 0; i < 3; i++) {
            length = teachers[i].length;
            node = new DefaultMutableTreeNode(gradeNames[i]);
            for (int j = 0; j < length; j++) {
                childNode = new DefaultMutableTreeNode(teachers[i][j]);
                node.add(childNode);
            }
            root.add(node);
        }
        JTree tree = new JTree(root);
        jPanel.add(tree);
        jPanel.setVisible(true);
        return jPanel;
    }
}
